Rubyにおける継承は、メソッドを共有するだけではなく、 状態の進化です。たとえば、 KaraokeSong < Songというサブクラスを作成すると、子クラスが親クラスの構造を継承しながら自らのアイデンティティを洗練させる「~である」関係を確立します。
1. 初期化チェーン
この super キーワードは世代間の橋渡しです。 initializeメソッド内で super を呼び出すことで、引数を親クラスのコンストラクタに引き渡し、基本的なインスタンス変数(@name、 @artist)が設定された後に、サブクラスが独自の状態(@lyrics)を追加できるようにします。
2. メソッドの拡張
たとえば to_s のようなメソッドをオーバーライドすることで、振る舞いを拡張できます。新しい定義内では super を呼び出すことで、親クラスの文字列出力を取得し、サブクラスの新しいデータを単純に追加するだけで、クリーンな進化的な経路を維持できます。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What is the primary function of the
super keyword in a subclass's initialize method?It clones the parent class into the child.
It passes arguments to the parent class's version of the method.
It deletes the instance variables of the parent.
It creates a global reference to the superclass.
✅ Correct!
Correct! super allows the parent class to handle the initialization of state it 'owns' before the child adds its own specific logic.❌ Incorrect
Recall that super is used to invoke the same-named method in the superclass.QUESTION 2
What happens to instance variables of a parent class during inheritance?
They are private and hidden from the subclass.
They are automatically populated even if 'super' is not called.
They become part of the subclass's state once the parent's initialize logic runs.
They are converted into class variables.
✅ Correct!
Instance variables are tied to the object; once initialized by the parent's code (via super), they exist within the subclass instance.❌ Incorrect
Instance variables aren't automatically populated; the initialization logic that sets them must be executed, usually via super.QUESTION 3
If you override
to_s in a subclass but do NOT call super, what happens?The parent's
to_s is still called automatically.The subclass implementation completely replaces the parent's implementation.
Ruby throws a SystemStackError.
The object defaults back to the standard
Object#to_s format.✅ Correct!
In Ruby, method overriding is total unless you explicitly use super to augment the parent's behavior.❌ Incorrect
Ruby does not automatically merge methods; it follows the lookup chain and stops at the first match.QUESTION 4
Which relationship best describes subclassing?
$Has-a$
$Is-a$
Contains-a
Provides-a
✅ Correct!
Inheritance defines an 'is-a' relationship (e.g., a KaraokeSong is a Song).❌ Incorrect
Composition is 'has-a'; inheritance is 'is-a'.QUESTION 5
What does the
inspect method reveal about an object's state?Only the public methods available.
The raw internal instance variables and their current values.
The source code of the class.
A list of all subclasses.
✅ Correct!
inspect is a debugging tool that shows the object's ID and all initialized instance variables.❌ Incorrect
inspect focuses on state (variables), not behavior (methods).State Evolution Analysis
Encapsulation vs. Tight Coupling
Consider a version of KaraokeSong where the developer implements to_s by directly interpolating the parent's variables: 'Song: #@name--#@artist (#@duration) [#@lyrics]'. Reading Context: We're correctly displaying the value of the @lyrics instance variable... The idea of a karaoke version of 'My Way' that lasts for 3,750 minutes is just too frightening to consider.
Q
So why is this a bad way to implement to_s? [Referencing the implementation of KaraokeSong#to_s that directly accesses parent instance variables]
Solution:
This is a bad implementation because it creates **tight coupling**. By directly accessing @name, @artist, and @duration (instance variables of the parent Song class), the subclass KaraokeSong becomes dependent on the internal implementation details of the parent. If the parent class changes its variable names (e.g., from @name to @title), the subclass breaks even though its own logic didn't change. It is better to use 'super' or call the parent's attribute methods to maintain **encapsulation** and ensure that changes in the parent class are automatically reflected in the subclass.
This is a bad implementation because it creates **tight coupling**. By directly accessing @name, @artist, and @duration (instance variables of the parent Song class), the subclass KaraokeSong becomes dependent on the internal implementation details of the parent. If the parent class changes its variable names (e.g., from @name to @title), the subclass breaks even though its own logic didn't change. It is better to use 'super' or call the parent's attribute methods to maintain **encapsulation** and ensure that changes in the parent class are automatically reflected in the subclass.
Q
How does using the 'super' keyword solve the issue described above?
Solution:
By using 'super' within the subclass's to_s method, you delegate the responsibility of formatting the basic song information back to the Song class. This ensures that if the Song class changes how it displays itself (e.g., changing the separator from '--' to ' by '), KaraokeSong automatically inherits that change without needing any manual code updates.
By using 'super' within the subclass's to_s method, you delegate the responsibility of formatting the basic song information back to the Song class. This ensures that if the Song class changes how it displays itself (e.g., changing the separator from '--' to ' by '), KaraokeSong automatically inherits that change without needing any manual code updates.